home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
array
/
vbarray.frm
< prev
next >
Wrap
Text File
|
1995-09-06
|
10KB
|
373 lines
VERSION 2.00
Begin Form VBArray
BorderStyle = 1 'Fixed Single
Caption = "VB Array"
ClientHeight = 3225
ClientLeft = 1305
ClientTop = 1500
ClientWidth = 6330
Height = 3630
Left = 1245
LinkMode = 1 'Source
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 3225
ScaleWidth = 6330
Top = 1155
Width = 6450
Begin CommandButton OkCancel
Cancel = -1 'True
Caption = "&Quit"
Height = 420
Index = 1
Left = 3600
TabIndex = 5
Top = 2280
Width = 1095
End
Begin CommandButton OkCancel
Caption = "&OK"
Default = -1 'True
Height = 420
Index = 0
Left = 1920
TabIndex = 4
Top = 2280
Width = 1095
End
Begin OptionButton OtherDemo
Caption = "&Some Type Array"
Height = 255
Left = 4200
TabIndex = 3
Top = 1800
Width = 1815
End
Begin OptionButton LngIntDemo
Caption = "&Long Integer Array"
Height = 255
Left = 2040
TabIndex = 2
Top = 1800
Width = 1935
End
Begin OptionButton IntDemo
Caption = "&Integer Array"
Height = 255
Left = 360
TabIndex = 1
Top = 1800
Value = -1 'True
Width = 1455
End
Begin ListBox ListBox
Height = 1200
Left = 360
TabIndex = 0
Top = 360
Width = 5655
End
Begin Label Status
Alignment = 2 'Center
BorderStyle = 1 'Fixed Single
Caption = "Status"
Height = 255
Left = -120
TabIndex = 6
Top = 3000
Width = 6615
End
End
DefInt A-Z
' VBARRAY *FAST* file I/O of arrays
' Address questions/comments/improvements to
' Costas Kitsos, CIS ID: 73667,1755
' Enjoy!
' holds a Windows supplied temp filename for the demo
Dim sTempFile As String
' listbox's hWnd so we can set tabs
Dim hWndListBox As Integer
' Flag for Application Initialization
Dim nInitApp As Integer
Function DoIntDemo () As Integer
' Declare variables
Dim tTempFile As OFSTRUCT ' Used by OpenFile function
ReDim nBefore(50) As Integer ' holds the Integers before I/O
ReDim nAfter(50) As Integer ' holds the Integers after I/O
Dim nBytes, hFileOut, hFileIn, nIObytes, nFclose ' Integers
Dim lMes As Long, lFileSize As Long
' Use the Random number generator to fill nBefore() with 50 integers
Randomize
For j = 1 To 50
nBefore(j) = Int((32767 - 1 + 1) * Rnd + 1)
Next
' ** I/O Starts here
' Create our temp file and open it for writing
hFileOut = OpenFile(sTempFile, tTempFile, OF_CREATE Or OF_WRITE)
' If we have a file handle proceed
If hFileOut <> 0 Then
' calculate the number of bytes to be written (NumberOfElements * Size)
' Since we want to write the entire array the formula would be:
' nBytes = UBound(Array) * Len(Array(element)). If we only needed
' twenty elements then nBytes = 20 * Len(Array(element)). Since we're
' dealing with fixed length arrays, element can be any legitimate
' array element.
nBytes = UBound(nBefore) * Len(nBefore(1))
' Write nBefore() to disk using API's lwrite function.
' We only need to pass the first array element. The nBytes
' parameter tells Windows how many bytes to write, or as far
' as we're concerned how many array elements.
nIObytes = lwrite(hFileOut, nBefore(1), nBytes)
' Get the file size with llseek. By specifying 0 for lOffset and
' and 2 for iOrigin we're saying seek position 0 from the end of
' the file. In other words, give us the FileSize.
lFileSize = llseek(hFileOut, 0, 2)
' close the output file.
nFclose = lclose(hFileOut)
' Now let's see if it worked. Open the file for reading.
hFileIn = OpenFile(sTempFile, tTempFile, OF_READ)
' We'll use the nAfter() array this time, nBytes is the same.
nIObytes = lread(hFileIn, nAfter(1), nBytes)
' close the input file.
nFclose = lclose(hFileIn)
' Let's prove that it worked. First, clear the list box.
lMes = SendMessage(hWndListBox, LB_RESETCONTENT, 0, ByVal 0&)
' Add a title.
ListBox.AddItem "Before" + Chr$(9) + "After"
' Add the nBefore() and nAfter() contents to the listbox
For j = 1 To 50
ListBox.AddItem LTrim$(Str$(nBefore(j))) + Chr$(9) + LTrim$(Str$(nAfter(j)))
Next
Status.Caption = "Temp File: " + sTempFile + Str$(lFileSize) + " bytes"
DoIntDemo = True ' success
Else
DoIntDemo = False ' failure
End If
End Function
Function DoLngIntDemo () As Integer
' Please see the DoIntDemo function for comments
Dim tTempFile As OFSTRUCT
ReDim lBefore(50) As Long
ReDim lAfter(50) As Long
Dim nBytes, hFileOut, hFileIn, nIObytes, nFclose
Dim lMes As Long, lFileSize As Long
Randomize
For j = 1 To 50
lBefore(j) = Int(1234532767 * Rnd + 1)
Next
hFileOut = OpenFile(sTempFile, tTempFile, OF_CREATE Or OF_WRITE)
If hFileOut <> 0 Then
nBytes = UBound(lBefore) * Len(lBefore(1))
nIObytes = lwrite(hFileOut, lBefore(1), nBytes)
lFileSize = llseek(hFileOut, 0, 2)
nFclose = lclose(hFileOut)
hFileIn = OpenFile(sTempFile, tTempFile, OF_READ)
nIObytes = lread(hFileIn, lAfter(1), nBytes)
nFclose = lclose(hFileIn)
lMes = SendMessage(hWndListBox, LB_RESETCONTENT, 0, ByVal 0&)
ListBox.AddItem "Before" + Chr$(9) + "After"
For j = 1 To 50
ListBox.AddItem LTrim$(Str$(lBefore(j))) + Chr$(9) + LTrim$(Str$(lAfter(j)))
Next
Status.Caption = "Temp File: " + sTempFile + Str$(lFileSize) + " bytes"
DoLngIntDemo = True
Else
DoLngIntDemo = False
End If
End Function
Function DoOtherDemo () As Integer
' Please see the DoIntDemo function for comments
Dim tTempFile As OFSTRUCT
ReDim tBefore(50) As SomeType
ReDim tAfter(50) As SomeType
Dim nBytes, hFileOut, hFileIn, nIObytes, nFclose
Dim lMes As Long, lFileSize As Long
Randomize
For j = 1 To 50
tBefore(j).nInteger = Int((32767 - 1 + 1) * Rnd + 1)
tBefore(j).sString = Chr$(Int((90 - 65 + 1) * Rnd + 65)) + "abcde"
tBefore(j).lLong = Int(1232767 * Rnd + 1)
Next
hFileOut = OpenFile(sTempFile, tTempFile, OF_CREATE Or OF_WRITE)
If hFileOut <> 0 Then
nBytes = UBound(tBefore) * Len(tBefore(1))
nIObytes = lwrite(hFileOut, tBefore(1), nBytes)
lFileSize = llseek(hFileOut, 0, 2)
nFclose = lclose(hFileOut)
hFileIn = OpenFile(sTempFile, tTempFile, OF_READ)
nIObytes = lread(hFileIn, tAfter(1), nBytes)
nFclose = lclose(hFileIn)
lMes = SendMessage(hWndListBox, LB_RESETCONTENT, 0, ByVal 0&)
ListBox.AddItem "Before" + Chr$(9) + "After"
For j = 1 To 50
Before$ = LTrim$(Str$(tBefore(j).nInteger)) + " " + tBefore(j).sString + Str$(tBefore(j).lLong)
After$ = LTrim$(Str$(tAfter(j).nInteger)) + " " + tAfter(j).sString + Str$(tAfter(j).lLong)
ListBox.AddItem Before$ + Chr$(9) + After$
Next
Status.Caption = "Temp File: " + sTempFile + Str$(lFileSize) + " bytes"
DoOtherDemo = True
Else
DoOtherDemo = False
End If
End Function
Sub Form_Load ()
sTempFile = String$(144, 0) ' Buffer
' Ask Windows for a temp file